//
//  LoginViewController.m
//  WonderfulNotes
//
//  Created by 千锋 on 16/1/11.
//  Copyright © 2016年 andy. All rights reserved.
//

#import "LoginViewController.h"
#import "loginButton.h"
#import "AppDelegate.h"

@interface LoginViewController ()

@property (weak, nonatomic) IBOutlet UITextField *userNameTF;
@property (weak, nonatomic) IBOutlet UITextField *passwordTF;
@property (weak, nonatomic) IBOutlet loginButton *loginBtn;

@property (nonatomic,strong) AppDelegate * globalApp;
@property (nonatomic,strong) NSUserDefaults * userDefaults;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"登陆";
    
    [self checkTF];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)userNameEditingChanged:(UITextField *)sender {
    
    [self checkTF];
}
- (IBAction)passwordEditingChanged:(UITextField *)sender {
    
    [self checkTF];
}

-(void)checkTF{
    
    if (self.userNameTF.text.length>0 && self.passwordTF.text.length>0) {
        
        self.loginBtn.enabled = YES;
        
    }else{
        
        self.loginBtn.enabled = NO;
    }

}

- (IBAction)loginBtnPressed:(loginButton *)sender {
    
    [SVProgressHUD showWithStatus:@"loggin..." maskType:SVProgressHUDMaskTypeClear];
    //将用户名保存到本地沙河路径
   // [[InfoStore sharedStore]setName:self.userNameTF.text forKey:@"UserName"];
    
    NSString * pwdMDStr = [EncryptionTools MD5StringFromString:self.passwordTF.text];
    
    //登陆
    [BmobUser loginWithUsernameInBackground:self.userNameTF.text password:pwdMDStr block:^(BmobUser *user, NSError *error) {
        
        if (user) {
            //登陆成功后需要,记下每一个用户的objectId
            //每一个用户的唯一标示
            NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
            [userDefaults setObject:@"exist" forKey:@"guidepage"];
            [userDefaults setObject:user.objectId forKey:@"userId"];
            [userDefaults setObject:user.username forKey:@"username"];
            
            [[NSNotificationCenter defaultCenter] postNotificationName:USER_REFRESH_NOTICE object:nil];
            
            //延迟两秒跳转
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                
            UIStoryboard * MainSb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            
            [self presentViewController:[MainSb instantiateViewControllerWithIdentifier:@"ResideViewController"] animated:YES completion:nil];
                
            });
        }else{
            
            NSString * msg = nil;
            if (error.code == 101) {
                msg = @"账号或密码错误";
            }else{
                
            }
            [SVProgressHUD showErrorWithStatus:msg];
        }
        
    }];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
